package share.storage.io.exchange;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import org.dom4j.Element;
import share.storage.io.File;
import share.storage.io.FileReader;
import share.storage.io.FileType;

public class ExFile implements File {

	private static final long serialVersionUID = -7245767590345450894L;

	long size = 0;
	long lastModified = 0;
	boolean isDirectory = false;
	String name = null;
	String describe = null;
	FileType type = null;
	Element  element=null;
	ExRootFile root=null;
	String uri=null;
	
	public ExFile(long size, long lastModified, boolean isDirectory,
			String name,Element element,ExRootFile root) {
		this.size = size;
		this.lastModified = lastModified;
		this.isDirectory = isDirectory;
		this.name = name;
		this.element=element;
		this.root=root;
		
		//get path
		File file=this;
		StringBuffer sb = new StringBuffer();
		while (file != null && file!= root) {
			try {
				sb.insert(0, "/" + URLEncoder.encode(file.getFilename(),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			file = file.getParent();
		}
		sb.insert(0, "/download");
		uri=sb.toString();
	}

	public long getSize() {
		return size;
	}

	public boolean isDirectory() {
		return isDirectory;
	}

	public boolean isFile() {
		return !isDirectory;
	}

	public File[] getChildren() {
		@SuppressWarnings("unchecked")
		List<Element> children=element.elements("File");
		File[] cs = new File[children.size()];
		for (int i = 0; i < cs.length; i++) {
			cs[i] = buildFile(children.get(i),root);
		}
		return cs;
	}


	public FileReader getReader() {
		return new ExFileReader(root.getHost()+uri);
	}

	public String getFilename() {
		return name;
	}

	public File getParent() {
		Element parent=element.getParent();
		if("File".equals(parent.getName()))
			return buildFile(parent,root);
		return root;
	}

	public void setParent(File parent) {
		//do nothing
	}

	public void setFileName(String name) {
		this.name = name;
	}

	public File findChildByName(String name) {
		return buildFile((Element) element.selectSingleNode("./File[@name='"+name+"']"),root);
	}

	public boolean isHidden() {
		return false;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public long lastModified() {
		return lastModified;
	}

	public FileType getFileType() {
		return type;
	}

	@Override
	public void setFileType(FileType type) {
		this.type = type;
	}

	/**
	 * 从XML元素生成文件
	 */
	public static File buildFile(Element e,ExRootFile root){
		try{
			String name=e.attributeValue("name");
			long size=Long.valueOf(e.attributeValue("size"));
			long lastModified=Long.valueOf(e.attributeValue("lastModified"));
			FileType type=FileType.valueOf(e.attributeValue("type"));
			boolean isDir=Boolean.valueOf(e.attributeValue("isDir"));
			File file=new ExFile(size,lastModified,isDir,name,e,root);
			file.setFileType(type);
			String describe=e.element("describe").getText();
			file.setDescribe(describe);
			return file;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
}
